package com.coderman.infosys.auth.app.facadeimpl.role;

import com.coderman.infosys.auth.app.convert.RoleConvert;
import com.coderman.infosys.auth.domain.bo.role.RoleBO;
import com.coderman.infosys.auth.domain.enums.errors.RoleErrorEnum;
import com.coderman.infosys.auth.domain.repository.UserRoleRepository;
import com.coderman.infosys.plat.auth.common.dto.ResultDataDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.coderman.infosys.auth.api.dto.role.RoleDTO;
import com.coderman.infosys.auth.api.facade.role.RoleFacade;
import java.util.List;
import com.coderman.infosys.auth.api.dto.role.request.CreateRoleRequestDTO;
import com.coderman.infosys.auth.api.dto.role.request.UpdateRoleRequestDTO;
import org.apache.dubbo.config.annotation.Service;


/**
 * @Description:角色接口实现类
 * @Author：
 * @CreateTime：2022-01-02 15:27:39
 * @version v1.0
 */
@Service
public class RoleFacadeImpl   implements RoleFacade{

	private  Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserRoleRepository userRoleRepository;

    @Override
    public ResultDataDto<Boolean> createRole(CreateRoleRequestDTO createRoleRequestDTO){
        RoleBO roleBO = RoleConvert.INSTANCE.dto2bo(createRoleRequestDTO);
        roleBO.relateToUser(createRoleRequestDTO.getUserIdList());
        //roleBO.buildRoleUserRelation(createRoleRequestDTO.getUserList());

        userRoleRepository.saveRole(roleBO);
        return ResultDataDto.success(true);
    }

    @Override
    public ResultDataDto<Boolean> updateRole(UpdateRoleRequestDTO updateRoleRequestDTO){
        RoleBO roleBO = RoleConvert.INSTANCE.dto2bo(updateRoleRequestDTO);
        int updateCount = userRoleRepository.updateRole(roleBO);
        if(updateCount > 0){
            return ResultDataDto.success(true);
        }
        return ResultDataDto.fail(RoleErrorEnum.ROLE_UPDATE_ERROR);
    }

    @Override
    public ResultDataDto<Boolean> disableRole(String roleCode) {
        Boolean b = userRoleRepository.disableRole(roleCode);
        if(b){
            return ResultDataDto.success(true);
        }
        return ResultDataDto.fail(RoleErrorEnum.ROLE_DISABLE_ERROR);
    }

    @Override
    public ResultDataDto<Boolean> enableRole(String roleCode) {
        Boolean b = userRoleRepository.enableRole(roleCode);
        if(b){
            return ResultDataDto.success(true);
        }
        return ResultDataDto.fail(RoleErrorEnum.ROLE_DISABLE_ERROR);
    }

  /*  @Override
    public ResultDataDto<Boolean> updateRoleStatus(String groupCode,Integer status){

        return null;
    }*/

    @Override
    public ResultDataDto<Boolean> relateToUser(List<Long> userIdList){

        return null;
    }

}